#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e6 + 5;

int n, m;
int a[N], v[N];  // a[i] != a[j]
ll ans[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i] >> v[i];
  stack<int> st;
  rep(i, 1, n) {
    while (st.size() && a[st.top()] < a[i]) {
      ans[i] += v[st.top()];
      st.pop();
    }
    if (st.size()) ans[st.top()] += v[i];
    st.push(i);
  }
  cout << *max_element(ans + 1, ans + n + 1);
  return 0;
}